/**
 * 
 */
package ro.utcn.tp.taskManagement.gui;

import java.awt.Container;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;

import ro.utcn.tp.taskManagement.business.ImageService;
import ro.utcn.tp.taskManagement.business.UsersManager;
import ro.utcn.tp.taskManagement.model.ImageDefs;
import ro.utcn.tp.taskManagement.model.ImagePanel;
import ro.utcn.tp.taskManagement.model.User;


/**
 * @author Coni
 * 
 */
public class CreateUserPage implements ActionListener {

	private JLabel jlbTitlu;

	private JLabel jlbUsername;

	private JLabel jlbPassword;

	private JLabel jlbRetypePassword;

	private JLabel jlbAdress;
	
	private JLabel jlbName;

	private JTextField jtfUsername;

	private JTextField jtfPassword;

	private JTextField jtfRetypePassword;

	private JTextField jtfAdress;
	
	private JTextField jtfName;

	private JButton jbnCreateUser;

	private JButton jbnCancel;

	private Container container;

	/** A reference to the image service. */

	private ImageService imageService;

	private static CreateUserPage instance;
	
	private User user;

	/**
	 * The constructor of the  class. initializes the screen.
	 */
	public CreateUserPage() {

		imageService = ImageService.getInstance();// instantiez imageservice
		createGUI();

		// instantiaza ce trebuie afisat
	}

	// imagine de background
	public void createGUI() {

		container = new ImagePanel(
				imageService.getImage(ImageDefs.APPLICATION_BACKGROUND_PATH));
		container.setLayout(new GridBagLayout());
		arrangeComponents();
	}

	public void arrangeComponents() {
		jlbTitlu = new JLabel("Create new account");

		jlbUsername = new JLabel("*Username:");
		jtfUsername = new JTextField("");
		jtfUsername.setEditable(true);

		jlbPassword = new JLabel("*Password:");
		jtfPassword = new JPasswordField("");
		jtfPassword.setEditable(true);

		jlbRetypePassword = new JLabel("*Retype password:");
		jtfRetypePassword = new JPasswordField("");
		jtfRetypePassword.setEditable(true);

		jlbAdress = new JLabel("Your adress:");
		jtfAdress = new JTextField("");
		jtfAdress.setEditable(true);
		
		jlbName = new JLabel("*Name:");
		jtfName = new JTextField("");
		jtfName.setEditable(true);

		jbnCreateUser = new JButton("Create account");
		jbnCancel = new JButton("Cancel");

		Font font = new Font("Times New Roman", Font.ITALIC, 30);
		jlbTitlu.setFont(font);

		GridBagConstraints gridBagConstraintsx01 = new GridBagConstraints();
		gridBagConstraintsx01.gridx = 0;
		gridBagConstraintsx01.gridy = 0;
		gridBagConstraintsx01.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx01.gridwidth = 2;
		gridBagConstraintsx01.fill = GridBagConstraints.BOTH;
		container.add(jlbTitlu, gridBagConstraintsx01);

		GridBagConstraints gridBagConstraintsx02 = new GridBagConstraints();
		gridBagConstraintsx02.gridx = 0;
		gridBagConstraintsx02.gridy = 1;
		gridBagConstraintsx02.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx02.gridwidth = 1;
		gridBagConstraintsx02.anchor = GridBagConstraints.WEST;
		container.add(jlbUsername, gridBagConstraintsx02);

		GridBagConstraints gridBagConstraintsx03 = new GridBagConstraints();
		gridBagConstraintsx03.gridx = 1;
		gridBagConstraintsx03.gridy = 1;
		gridBagConstraintsx03.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx03.gridwidth = 1;
		gridBagConstraintsx03.gridheight = 1;
		gridBagConstraintsx03.fill = GridBagConstraints.BOTH;
		container.add(jtfUsername, gridBagConstraintsx03);

		GridBagConstraints gridBagConstraintsx04 = new GridBagConstraints();
		gridBagConstraintsx04.gridx = 0;
		gridBagConstraintsx04.gridy = 2;
		gridBagConstraintsx04.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx04.gridwidth = 1;
		gridBagConstraintsx04.anchor = GridBagConstraints.WEST;
		container.add(jlbPassword, gridBagConstraintsx04);

		GridBagConstraints gridBagConstraintsx05 = new GridBagConstraints();
		gridBagConstraintsx05.gridx = 1;
		gridBagConstraintsx05.gridy = 2;
		gridBagConstraintsx05.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx05.gridwidth = 1;
		gridBagConstraintsx05.gridheight = 1;
		gridBagConstraintsx05.fill = GridBagConstraints.BOTH;
		container.add(jtfPassword, gridBagConstraintsx05);

		GridBagConstraints gridBagConstraintsx06 = new GridBagConstraints();
		gridBagConstraintsx06.gridx = 0;
		gridBagConstraintsx06.gridy = 3;
		gridBagConstraintsx06.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx06.gridwidth = 1;
		gridBagConstraintsx06.anchor = GridBagConstraints.WEST;
		container.add(jlbRetypePassword, gridBagConstraintsx06);

		GridBagConstraints gridBagConstraintsx07 = new GridBagConstraints();
		gridBagConstraintsx07.gridx = 1;
		gridBagConstraintsx07.gridy = 3;
		gridBagConstraintsx07.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx07.gridwidth = 1;
		gridBagConstraintsx07.gridheight = 1;
		gridBagConstraintsx07.fill = GridBagConstraints.BOTH;
		container.add(jtfRetypePassword, gridBagConstraintsx07);
		
		GridBagConstraints gridBagConstraintsx12 = new GridBagConstraints();
		gridBagConstraintsx12.gridx = 0;
		gridBagConstraintsx12.gridy = 4;
		gridBagConstraintsx12.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx12.gridwidth = 1;
		gridBagConstraintsx12.anchor = GridBagConstraints.WEST;
		container.add(jlbName, gridBagConstraintsx12);

		GridBagConstraints gridBagConstraintsx13 = new GridBagConstraints();
		gridBagConstraintsx13.gridx = 1;
		gridBagConstraintsx13.gridy = 4;
		gridBagConstraintsx13.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx13.gridwidth = 1;
		gridBagConstraintsx13.gridheight = 1;
		gridBagConstraintsx13.fill = GridBagConstraints.BOTH;
		container.add(jtfName, gridBagConstraintsx13);

		GridBagConstraints gridBagConstraintsx08 = new GridBagConstraints();
		gridBagConstraintsx08.gridx = 0;
		gridBagConstraintsx08.gridy = 5;
		gridBagConstraintsx08.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx08.gridwidth = 1;
		gridBagConstraintsx08.anchor = GridBagConstraints.WEST;
		container.add(jlbAdress, gridBagConstraintsx08);

		GridBagConstraints gridBagConstraintsx09 = new GridBagConstraints();
		gridBagConstraintsx09.gridx = 1;
		gridBagConstraintsx09.gridy = 5;
		gridBagConstraintsx09.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx09.gridwidth = 1;
		gridBagConstraintsx09.gridheight = 1;
		gridBagConstraintsx09.fill = GridBagConstraints.BOTH;
		container.add(jtfAdress, gridBagConstraintsx09);

		GridBagConstraints gridBagConstraintsx10 = new GridBagConstraints();
		gridBagConstraintsx10.gridx = 0;
		gridBagConstraintsx10.gridy = 6;
		gridBagConstraintsx10.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx10.gridwidth = 1;
		gridBagConstraintsx10.fill = GridBagConstraints.BOTH;
		container.add(jbnCreateUser, gridBagConstraintsx10);

		GridBagConstraints gridBagConstraintsx11 = new GridBagConstraints();
		gridBagConstraintsx11.gridx = 1;
		gridBagConstraintsx11.gridy = 6;
		gridBagConstraintsx11.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx11.gridwidth = 1;
		gridBagConstraintsx11.fill = GridBagConstraints.BOTH;
		container.add(jbnCancel, gridBagConstraintsx11);

		jbnCreateUser.setEnabled(true);
		jbnCancel.setEnabled(true);
		jbnCreateUser.addActionListener(this);
		jbnCancel.addActionListener(this);

	}

	public void stateChanged(ChangeEvent arg0) {

	}

	public static CreateUserPage getNewInstance() {
		instance = new CreateUserPage();
		return instance;
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		try {
			if (e.getSource() == jbnCreateUser) {
				createUser();
			} else if (e.getSource() == jbnCancel) {
				cancel();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private void cancel() {
		ApplicationScreen.getInstance().setContainer(
				LoginPage.getNewInstance().getContainer());
	}
/**
 * creare cont utilizator nou
 * validare campuri
 * creare customer
 */
	private void createUser() {
		// validate username!=empty, password!=empty, password==retypePassword
		String usernameText = jtfUsername.getText().trim();
		if (usernameText.isEmpty()) {
			// afiseaza mesaj ca username nu a fost introdus
			JOptionPane.showMessageDialog(ApplicationScreen
					.getInstance().getFrame(),
					"Va rugam introduceti nume utilizator!",
					"Data Input Error", JOptionPane.ERROR_MESSAGE);
			return;
		}
		String passwordText = jtfPassword.getText().trim();
		if (passwordText.isEmpty()) {
			// afiseaza mesaj ca parola nu a fost introdusa
			JOptionPane.showMessageDialog(ApplicationScreen
					.getInstance().getFrame(),
					"Va rugam introduceti parola!",
					"Data Input Error", JOptionPane.ERROR_MESSAGE);
			return;
		}
		String retypePasswordText = jtfRetypePassword.getText().trim();
		if (retypePasswordText.isEmpty()) {
			// afiseaza mesaj ca parola nu a fost reintrodusa pt a fi verificata
			JOptionPane.showMessageDialog(ApplicationScreen
					.getInstance().getFrame(),
					"Va rugam reintroduceti parola!",
					"Data Input Error", JOptionPane.ERROR_MESSAGE);
			return;
		}
		if (!passwordText.equals(retypePasswordText)) {
		     //afiseaza mesaj ca parola reintrodusa nu corespunde cu prima parola
			JOptionPane.showMessageDialog(ApplicationScreen
					.getInstance().getFrame(),
					"Parola nu a fost reintrodusa corect!",
					"Data Input Error", JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		// create user and validate it
		user = new User();
		user.setUsername(jtfUsername.getText());
		user.setPassword(jtfPassword.getText());
		user.setAddress(jtfAdress.getText());
		user.setName(jtfName.getText());
		user.setRole(User.USER_ROLE);
		boolean success = UsersManager.getInstance().createUser(user);
		if (success) {
			ApplicationScreen.getInstance().setContainer(
					UserPage.getNewInstance(user).getContainer());
		} else {
			JOptionPane
					.showMessageDialog(
							ApplicationScreen.getInstance().getFrame(),
							"Exista deja un utilizator cu acest username. Introduceti alt username!",
							"Data Input Error", JOptionPane.ERROR_MESSAGE);
		}

	}

	/**
	 * @return
	 */
	public Container getContainer() {
		return container;
	}

}
